<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!--table表格-->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="name"></el-table-column>
        <el-table-column label="所属分类" prop="cateName" width="100px"></el-table-column>
        <el-table-column label="价格(金币)" prop="price" width="100px"></el-table-column>
        <el-table-column label="数量(个)" prop="stock" width="100px"></el-table-column>
        <el-table-column label="发布时间" prop="createTime" width="160px"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数对象
      queryInfo: {
        pageNo: 1,
        pageSize: 10,
        query: ''
      },
      // 商品列表数据
      goodsList: [],
      // 商品总条数
      total: 0
    }
  },
  created() {
    // 获取所有商品数据
    this.getGoodsList()
  },
  methods: {
    // 分页获取所有商品数据
    async getGoodsList() {
      const result = await this.$http.get('/goods/goodList', { params: this.queryInfo })
      if (result.data.code !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = result.data.data.data
      this.total = result.data.data.total
    },
    // 分页数量发生变化
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getGoodsList()
    },
    // 分页页码发生变化
    handleCurrentChange(newPageNo) {
      this.queryInfo.pageNo = newPageNo
      this.getGoodsList()
    },
    // 删除商品
    async removeById(id) {
      // 弹框询问是否删除用户
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果用户确认删除，则返回字符串 confirm
      // 如果用户取消删除，则返回字符串 cancel
      if (result !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const res = await this.$http.delete(`/goods/delete/${id}`)
      if (res.data.code !== 200) {
        return this.$message.error('删除商品失败！')
      }
      this.$message.success('删除商品成功！')
      this.getGoodsList()
    },
    // 跳转到添加商品页面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>

</style>
